package com.weilx959.spt.plugin.e2sp.utils.sql.deparser;

import com.weilx959.spt.core.utils.Assert;
import com.weilx959.spt.plugin.e2sp.model.statement.InsertStatement;
import com.weilx959.spt.plugin.e2sp.model.statement.Statement;
import com.weilx959.spt.plugin.e2sp.utils.sql.SqlUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.insert.Insert;

/**
 * insert sql语句解析器
 *
 * @author weilx
 * @since 2024-07-02
 */
public class InsertSQLDeParser implements SQLDeParser<Insert> {
    @Override
    public Statement deParse(Insert insert) {
        Assert.notNull(insert, "Insert statement must not be null");
        InsertStatement insertStatement = new InsertStatement();
        insertStatement.setSchema(insert.getTable().getSchemaName());
        insertStatement.setTableName(insert.getTable().getName());
        insertStatement.setColumns(insert.getColumns().stream().map(Column::getColumnName).toList());
        insertStatement.setValues(insert.getValues().getExpressions().stream().map(SqlUtil::getValue).toList());
        insertStatement.setInsert(insert);
        insertStatement.setValue(insert.getValues());
        return insertStatement;
    }
}
